<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\wh\builder;

use app\wh\model\StockType as StockTypeModel;
use think\Db;


/**
 * 后台公共控制器
 * @package app\admin\controller
 */
class Admin extends \app\mill\builder\Admin
{
    /**
     * 初始化
     * @author 蔡伟明 <314013107@qq.com>
     * @throws \think\Exception
     */
    protected function initialize()
    {
        parent::initialize();

        $this->getStockMenu();
    }

    //重置仓库、采购的侧边栏菜单，按物料分类加入子导航
    protected function getStockMenu(){
        //仓库菜单的ID：386，采购菜单的ID：403
        $menu_id = [386,403];
        // 获取侧边栏菜单
        $_sidebar_menus = $this->getSidebarMenu();
        // 获取面包屑导航
        $_location = $this->getLocation('', true);
        if(in_array($_location[0]['id'],$menu_id))$menu_id = [$_location[0]['id']];

        $_type_id = input('stock_type');
        if(empty($_type_id))$_type_id = StockTypeModel::getTypePidFirst();

        //$_sidebar_menus = [$_sidebar_menus[1]];
        //自动加入物料分类：0：不加入 1：二级 2：同级
        //仅限二级三级菜单
        foreach ($_sidebar_menus as &$menu){
            if(!empty($menu['child'])){
                $i = 0;
                foreach ($menu['child'] as &$m){
                    //dump($m['wh_type']);exit;
                    //二级
                    if(isset($m['wh_type']) && $m['wh_type'] == 1){
                        //dump($m);exit;
                        $m['wh_type'] = 0;
                        $url_value = Db::name('admin_menu')->where(['id'=>$m['id']])->value('url_value');
                        $list_tab = StockTypeModel::getTabNav($url_value,0,$m);
                        //定位菜单
                        if(!empty($_location[1]['id']) && $_location[1]['id'] == $m['id']){
                            foreach ($list_tab as &$t){
                                if($t['id'] == "{$m['id']}_stock_type_{$_type_id}"){
                                    $_location[2] = $_location[1];
                                    $_location[2]['id'] = $t['id'];
                                    $_location[2]['pid'] = $_location[1]['id'];
                                    $_location[2]['title'] = $t['title'];
                                }
                            }
                        }

                        if(!isset($m['child']))$m['child'] = [];
                        $m['child'] = array_merge($m['child'],$list_tab);
                    }
                    //同级
                    else if(isset($m['wh_type']) && $m['wh_type'] == 2){
                        $m['wh_type'] = 0;
                        $url_value = Db::name('admin_menu')->where(['id'=>$m['id']])->value('url_value');
                        $list_tab = StockTypeModel::getTabNav($url_value,0,$m);
                        //定位菜单
                        if(!empty($_location[1]['id']) && $_location[1]['id'] == $m['id']){
                            foreach ($list_tab as &$t){
                                if($t['id'] == "{$m['id']}_stock_type_{$_type_id}"){
                                    $_location[1]['id'] = $t['id'];
                                    //$_location[1]['pid'] = $_location[1]['id'];
                                    $_location[1]['title'] = $t['title'];
                                }
                            }
                        }

                        $m['online_hide'] = 1;
                        $menu['child'] = array_merge($list_tab,$menu['child']);
                    }

                    $i++;
                }
            }
        }
        //dump($_sidebar_menus);
        //dump($_location);
        //exit;
        /*//仓库菜单的ID：386
        if($_location[0]['id'] == $menu_id[0]){
            foreach ($_sidebar_menus as &$menu){
                if($menu['id'] == $menu_id[0]){
                    if(!empty($menu['child'])){
                        foreach ($menu['child'] as &$m){
                            $url_value = Db::name('admin_menu')->where(['id'=>$m['id']])->value('url_value');
                            $list_tab = StockTypeModel::getTabNav($url_value,0,$m);
                            //定位菜单
                            if(!empty($_location[1]['id']) && $_location[1]['id'] == $m['id']){
                                foreach ($list_tab as &$t){
                                    if($t['id'] == "{$m['id']}_stock_type_{$_type_id}"){
                                        $_location[2] = $_location[1];
                                        $_location[2]['id'] = $t['id'];
                                        $_location[2]['pid'] = $_location[1]['id'];
                                        $_location[2]['title'] = $t['title'];
                                    }
                                }
                            }

                            if(!isset($m['child']))$m['child'] = [];
                            $m['child'] = array_merge($m['child'],$list_tab);
                        }
                    }
                }
            }
        }*/

        // 获取侧边栏菜单
        $this->assign('_sidebar_menus',$_sidebar_menus);
        // 获取面包屑导航
        $this->assign('_location', $_location);
    }


}
